package com.base.controller;

import java.io.PrintWriter;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.base.comm.CommUrls;
import com.base.helper.WebHelper;
import com.base.po.param.NetState;
import com.base.po.voip.Rewards;
import com.base.po.voip.Subscribers;
import com.base.po.voip.Userpays;
import com.base.service.*;
import net.sf.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import com.base.comm.CommContent;
import com.base.po.voip.SelfShopOrder;
import com.base.utils.IP;
import com.base.utils.Produce;

@Controller
@RequestMapping("/admin")
public class update_SelfShopOrder {
	@Autowired
	private LogsService logsService;// log日志
	@Autowired
	private SelfShopOrderService selfShopOrderService;
	@Autowired
	private SubscribersService subscribersService;
	@Autowired
	private UserpaysService userpaysService;
	@Autowired
	private RewardsService rewardsService;
	
	String SUCCESS = "success";

	// 更改商户信息
	@RequestMapping(value = "/updateSelfShopOrder", method = RequestMethod.POST)
	public void updateSelfShopOrder(HttpServletRequest request, HttpServletResponse response,
			@RequestParam(value = "id", required = true) String id,  String expressnumber,
			Byte deliverystatus, String expresscode, Byte datastatus, Integer istakeover) throws Exception {
		// 返回参数
		response.setCharacterEncoding("utf-8");
		response.setContentType("text/html;charset=UTF-8");
//		String adminUserId = (String) request.getSession().getAttribute("adminUserId");
	
		SelfShopOrder selfShopOrder = selfShopOrderService.selectByPrimaryKey(id);
		PrintWriter out = response.getWriter();
		
		if (id == null) {
			out.print("浏览器在点击的过程中发现丢失了用户信息，请您刷新重试！");
			return;
		} else {
			selfShopOrder.setExpresscode(expresscode);
			selfShopOrder.setExpressnumber(expressnumber);
			selfShopOrder.setDeliverystatus(deliverystatus);
			selfShopOrder.setDeliverytime(Produce.getNowDate());
			selfShopOrder.setDatastatus(datastatus);
			selfShopOrder.setIstakeover(istakeover);
			int code = selfShopOrderService.updateByPrimaryKeySelective(selfShopOrder);
			String usernamesys = (String) request.getSession().getAttribute("useracc");
			if (code == 1) {
				logsService.insert(CommContent.TYPE_UPDATE, "修改" + selfShopOrder.getOrdernum() + "发货成功", usernamesys, true,
						IP.getIpAddr(request));
				out.print(SUCCESS);
			} else {
				logsService.insert(CommContent.TYPE_UPDATE, "修改" + selfShopOrder.getOrdernum() + "发货失败", usernamesys, true,
						IP.getIpAddr(request));
				out.print("更改失败，请您重试");
			}
		}
	}

	// 退款接口
	@RequestMapping(value = "/refundSelfShopOrder", method = RequestMethod.POST)
	public void refundSelfShopOrder(HttpServletRequest request, HttpServletResponse response,
									@RequestParam(value = "orderid", required = true) String orderid, Integer istakeover) throws Exception {
		// 返回参数
		response.setCharacterEncoding("utf-8");
		response.setContentType("text/html;charset=UTF-8");
		PrintWriter out = response.getWriter();

		if (orderid == null) {
			out.print("浏览器在点击的过程中发现丢失了用户信息，请您刷新重试！");
			return;
		}

		NetState netState = new NetState();
		String json = null;
		String param = null;
		Rewards rewards = null;
		SelfShopOrder selfShopOrder = selfShopOrderService.selectByOrderID(orderid);
		List<Rewards> rewardsList = rewardsService.selectByOrderId(orderid);
		if (rewardsList != null && rewardsList.size() > 0){
			rewards = rewardsList.get(0);
		}

		if (selfShopOrder != null){
			if (istakeover == 1){
				int orderCode = 0;

				Userpays userpays = userpaysService.selectByUserID(selfShopOrder.getUserid(), true);
				if (userpays.getAliaccount() != null && !userpays.getAliaccount().equals("")){
					param = "userId=" + selfShopOrder.getUserid() + "&parentId=" + selfShopOrder.getParentid() + "&orderId=" + selfShopOrder.getOrderid();
					json = WebHelper.PostData(CommUrls.THISSERVER + "aliPayment/shopRefund", param, null, null);

					netState = (NetState) JSONObject.toBean(JSONObject.fromObject(json), NetState.class);
					if (netState.getErrorCode() == 2000){
						// 更新订单状态
						selfShopOrder.setIstakeover(3);
						selfShopOrder.setRemarks("退款成功，已退款到支付宝");
						orderCode = selfShopOrderService.updateByPrimaryKeySelective(selfShopOrder);

						if (rewards != null){
							rewards.setIsrecharge(2);
							rewardsService.updateByPrimaryKeySelective(rewards);
						}

						if (orderCode == 1){
							out.print("退款成功，已退款到支付宝");
						}else {
							out.print("退款成功，订单状态变更失败");
						}
					}else{
						out.print("退款失败,失败原因" + netState.getData());
					}
				}else if(userpays.getBankno() != null && !userpays.getBankno().equals("")){
					param = "userId=" + selfShopOrder.getUserid() + "&parentId=" + selfShopOrder.getParentid() + "&orderId=" + selfShopOrder.getOrderid();
					json = WebHelper.PostData(CommUrls.THISSERVER + "lianlian/shopRefund", param, null, null);

					netState = (NetState) JSONObject.toBean(JSONObject.fromObject(json), NetState.class);
					if (netState.getErrorCode() == 2000){
						selfShopOrder.setIstakeover(3);
						selfShopOrder.setRemarks("退款成功，已退款到银行卡");
						orderCode = selfShopOrderService.updateByPrimaryKeySelective(selfShopOrder);

						if (rewards != null){
							rewards.setIsrecharge(2);
							rewardsService.updateByPrimaryKeySelective(rewards);
						}

						if (orderCode == 1){
							out.print("退款成功，已退款到银行卡");
						}else {
							out.print("退款成功，订单状态变更失败");
						}
					}else{
						out.print("退款失败,失败原因" + netState.getData());
					}
				}else{
					int refundCode = rewardsService.insert(new Rewards(9, selfShopOrder.getParentid(), selfShopOrder.getUserid(), selfShopOrder.getUserid(),
							selfShopOrder.getCash().doubleValue(), 1, Produce.getNowDate(), 1));
					if (refundCode == 1){
						selfShopOrder.setIstakeover(3);
						selfShopOrder.setRemarks("退款成功，已退款到佣金");
						orderCode = selfShopOrderService.updateByPrimaryKeySelective(selfShopOrder);

						if (rewards != null){
							rewards.setIsrecharge(2);
							rewardsService.updateByPrimaryKeySelective(rewards);
						}

						if (orderCode == 1){
							out.print("退款成功，已退款到佣金");
						}else {
							out.print("退款成功，订单状态变更失败");
						}
					}else {
						out.print("退款失败");
					}
				}
			}else {
				selfShopOrder.setIstakeover(0);
				int orderCode = selfShopOrderService.updateByPrimaryKeySelective(selfShopOrder);
				if (orderCode == 1){
					out.print("退款申请驳回成功，订单状态修改成功");
				}else {
					out.print("退款申请驳回失败，订单状态修改失败");
				}
			}
		}else{
			out.print("订单查询失败");
		}
	}
}
